/*
*
* Created by yuri at Apr 1, 2011
*
*/



package implementation.shared;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class CPacket implements IPacket {
	
	private static final long serialVersionUID = -5384862921136779893L;
	
	private EPacketType _type;
	private Map<EPacketParam, Object> _params;
	
	public CPacket(EPacketType type) {
		this._type = type;
		this._params = new HashMap<EPacketParam,Object>();
	}

	@Override
	public EPacketType getType() {
		return this._type;
	}

	@Override
	public Map<EPacketParam, Object> getParams() {
		return this._params;
	}
	
	@Override
	public void addParam(EPacketParam paramName, Object param) {
		this._params.put(paramName, param);
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(this._type + "\n");
		for (Iterator<Map.Entry<EPacketParam, Object>> iterator = this._params.entrySet().iterator(); iterator.hasNext();) {
			Map.Entry<EPacketParam, Object> entry = (Map.Entry<EPacketParam, Object>) iterator.next();
			 sb.append(entry.getKey().toString() +":"+entry.getValue()+"\n");
			
		}
		return sb.toString();
	}

	@Override
	public Object getParam(EPacketParam paramName) {
		return this._params.get(paramName);
	}

	@Override
	public boolean isSuccessful() {
		return this._type == EPacketType.ESuccess;
	}

	@Override
	public boolean isFailure() {
		return this._type == EPacketType.EFailure;
	}

}
